package com.itxzh.poidemo.service.impl;

import com.itxzh.common.entities.Person;
import com.itxzh.poidemo.dao.PersonMapper;
import com.itxzh.poidemo.service.PersonService;
import lombok.SneakyThrows;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

/**
 * Description
 *
 * @Author ldt
 * @Date 2022/5/9 9:57
 * @Version 1.0.0
 */
@Service
@Slf4j
public class PersonServiceImpl implements PersonService {

    @Autowired
    PersonMapper personMapper;

    @Override
    public List<Person> getAll() {
        return personMapper.getAll();
    }

    @Override
    public Integer create(Person person) {
        return personMapper.create(person);
    }

    @Override
    public Integer insertBatch(List<Person> personList) {
        return personMapper.insertBatch(personList);
    }

    @Override
    @SneakyThrows
    public void bigExport(HttpServletResponse response) {

        response.setHeader("Content-disposition", "attachment; filename="
                + new String(("person_list" + ".xlsx").getBytes("utf-8"), "ISO8859-1"));//设置下载的文件名

        OutputStream out = null;
        
        SXSSFWorkbook book = new SXSSFWorkbook(100); //超100行将写入临时文件
        book.setCompressTempFiles(false); //是否压缩临时文件，否则写入速度更快，但更占磁盘，但程序最后是会将临时文件删掉的

        SXSSFSheet sheet = book.createSheet("person1");
        
        // 设置表头
        SXSSFRow headRow = sheet.createRow(0);
        headRow.createCell(0).setCellValue("ID");
        headRow.createCell(1).setCellValue("NAME");
        headRow.createCell(2).setCellValue("SEX");
        headRow.createCell(3).setCellValue("AGE");

        // 流式查询，低内存获取百万数据
        personMapper.scan(new ResultHandler<Person>(){
            @Override
            public void handleResult(ResultContext<? extends Person> resultContext) {
                int resultCount = resultContext.getResultCount();
                Person person = resultContext.getResultObject();
                log.debug(person.toString());
                SXSSFRow row = sheet.createRow(resultCount);
                row.createCell(0).setCellValue(person.getId());
                row.createCell(1).setCellValue(person.getName());
                row.createCell(2).setCellValue(person.getSex());
                row.createCell(3).setCellValue(person.getAge());
            }
        });

        out = response.getOutputStream();
        book.write(out);

        if (book != null) {
            book.dispose(); // 删除临时文件，很重要，否则磁盘可能会被写满
        }

        if(out != null) {
            out.close();
        }
    }

    @Override
    @SneakyThrows
    public void smallExport(HttpServletResponse response) {
        response.setHeader("Content-disposition", "attachment; filename="
                + new String(("person_list" + ".xlsx").getBytes("utf-8"), "ISO8859-1"));//设置下载的文件名

        OutputStream out = null;

        XSSFWorkbook book = new XSSFWorkbook();

        XSSFSheet sheet = book.createSheet("person1");

        // 设置表头
        XSSFRow headRow = sheet.createRow(0);
        headRow.createCell(0).setCellValue("ID");
        headRow.createCell(1).setCellValue("NAME");
        headRow.createCell(2).setCellValue("SEX");
        headRow.createCell(3).setCellValue("AGE");

        List<Person> personList = personMapper.getAll();

        for (int i = 0; i < personList.size(); i++) {
            Person person = personList.get(i);
            log.debug(person.toString());
            XSSFRow row = sheet.createRow((i+1));
            row.createCell(0).setCellValue(person.getId());
            row.createCell(1).setCellValue(person.getName());
            row.createCell(2).setCellValue(person.getSex());
            row.createCell(3).setCellValue(person.getAge());
        }

        out = response.getOutputStream();
        book.write(out);

        if (book != null) {
            book.close();// 删除临时文件，很重要，否则磁盘可能会被写满
        }

        if(out != null) {
            out.close();
        }
    }
}
